package com.snowfeel.sfmall.sys.controller;

import com.snowfeel.sfmall.core.param.IdParam;
import com.snowfeel.sfmall.core.param.IdsParam;
import com.snowfeel.sfmall.core.response.JsonResult;
import com.snowfeel.sfmall.core.vo.Option;
import com.snowfeel.sfmall.core.vo.PageVo;
import com.snowfeel.sfmall.sys.dto.SysDictTypeParam;
import com.snowfeel.sfmall.sys.query.SysDictTypeQuery;
import com.snowfeel.sfmall.sys.service.SysDictTypeService;
import com.snowfeel.sfmall.sys.vo.SysDictTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "字典类型接口")
@RestController
@RequestMapping("/dict/types")
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService dictTypeService;

    @ApiOperation(value = "字典类型分页列表")
    @GetMapping("/page")
    public JsonResult<PageVo<SysDictTypeVo>> page(@RequestBody SysDictTypeQuery query) {
        PageVo<SysDictTypeVo> result = dictTypeService.page(query);
        return JsonResult.success(result);
    }

    @ApiOperation(value = "字典类型表单详情")
    @GetMapping("/detail")
    public JsonResult<SysDictTypeVo> detail(@ApiParam("字典ID") @RequestBody IdParam param) {
        SysDictTypeVo dictTypeVo = dictTypeService.detail(param);
        return JsonResult.success(dictTypeVo);
    }

    @ApiOperation(value = "新增字典类型")
    @PostMapping("/create")
    public JsonResult create(@RequestBody SysDictTypeParam dictTypeParam) {
        boolean result = dictTypeService.create(dictTypeParam);
        return JsonResult.success(result);
    }

    @ApiOperation(value = "修改字典类型")
    @PostMapping("/update/{id}")
    public JsonResult update(@PathVariable Long id, @RequestBody SysDictTypeParam dictTypeParam) {
        boolean status = dictTypeService.update(id, dictTypeParam);
        return JsonResult.success(status);
    }

    @ApiOperation(value = "删除字典类型")
    @PostMapping("/deleteBatch")
    public JsonResult deleteBatch(
            @ApiParam("字典类型ID列表") @RequestBody IdsParam param
            ) {
        boolean result = dictTypeService.deleteBatch(param);
        return JsonResult.success(result);
    }

    @ApiOperation(value = "获取字典类型的数据项")
    @GetMapping("/{typeCode}/options")
    public JsonResult<List<Option>> options(
            @ApiParam("字典类型编码") @PathVariable String typeCode
    ) {
        List<Option> list = dictTypeService.options(typeCode);
        return JsonResult.success(list);
    }
}
